package com.bkgy.modular.subjectQuery.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bkgy.modular.subjectQuery.entity.SideData1720;
import com.bkgy.modular.subjectQuery.entity.SideData2130;
import com.bkgy.modular.subjectQuery.entity.SideData2130Cal;
import com.bkgy.modular.subjectQuery.mapper.SideData1720Mapper;
import com.bkgy.modular.subjectQuery.mapper.SideData2130CalMapper;
import com.bkgy.modular.subjectQuery.mapper.SideData2130Mapper;
import com.bkgy.modular.subjectQuery.service.SideDataService;
import com.bkgy.sys.common.entity.ResultData;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author ZhuHongKai
 * @version 1.0
 * @ClassName SideDataServiceImpl
 * @date 2023/1/7 10:06
 */
@Service
public class SideDataServiceImpl extends ServiceImpl<SideData1720Mapper, SideData1720> implements SideDataService {
    @Resource
    private SideData1720Mapper sideData1720Mapper;

    @Resource
    private SideData2130Mapper sideData2130Mapper;

    @Resource
    private SideData2130CalMapper sideData2130CalMapper;

    /**
     * 主题查询数据查询接口
     * @param jsonObject
     * @return
     */
    @Override
    public ResultData findByData(JSONObject jsonObject) {
        // 接收前端传进参数
        // 分页参数
        Integer pageIndex = jsonObject.getInteger("pageIndex");
        Integer pageSize = jsonObject.getInteger("pageSize");

        // 查询条件参数对象
        JSONObject condition = jsonObject.getJSONObject("condition");

        String coil_id = condition.getString("coil_id"); // 出口卷号
        String productionLine = condition.getString("productionLine"); // 产线
        String dbegin = condition.getString("dbegin"); // 开始时间
        String dend = condition.getString("dend"); // 结束时间

        // 1720酸轧
        if (productionLine.equals("1")) {
            // 构造查询条件对象
            LambdaQueryWrapper<SideData1720> queryWrapper = new LambdaQueryWrapper<>();
            // 判断condition不为空的情况下进入判断
            if (!condition.isEmpty()) {
                // 判断出口卷号是否存在,存在进入判断添加条件
                if (StringUtils.hasText(coil_id)) {
                    queryWrapper.eq(SideData1720::getCoil_id,coil_id);
                }
                // 判断开始时间参数是否存在,存在进入判断添加条件
                if (StringUtils.hasText(dbegin) && StringUtils.hasText(dend)) {
                    // getProcess_end_time大于开始时间
                    queryWrapper.ge(SideData1720::getProcess_end_time,dbegin);
                    // getProcess_end_time小于于结束时间
                    queryWrapper.le(SideData1720::getProcess_end_time,dend);
                    queryWrapper.orderByDesc(SideData1720::getProcess_end_time);
                }
            }
            // 创建分页构造器
            PageHelper.startPage(pageIndex,pageSize);
            List<SideData1720> data1720List = sideData1720Mapper.selectList(queryWrapper);
            PageInfo pageInfo = new PageInfo(data1720List);
            return ResultData.ResultDataFaultSelf("查询成功",pageInfo);
        // 2130 酸轧
        } else if (productionLine.equals("2")){
            LambdaQueryWrapper<SideData2130> queryWrapper = new LambdaQueryWrapper<>();
            // 判断condition不为空的情况下进入判断
            if (!condition.isEmpty()) {
                // 判断出口卷号是否存在,存在进入判断添加条件
                if (StringUtils.hasText(coil_id)) {
                    queryWrapper.eq(SideData2130::getCoil_id,coil_id);
                }
                // 判断开始时间参数是否存在,存在进入判断添加条件
                if (StringUtils.hasText(dbegin) && StringUtils.hasText(dend)) {
                    // getProcess_end_time大于开始时间
                    queryWrapper.ge(SideData2130::getProcess_end_time,dbegin);
                    // getProcess_end_time小于于结束时间
                    queryWrapper.le(SideData2130::getProcess_end_time,dend);
                    queryWrapper.orderByDesc(SideData2130::getProcess_end_time);
                }
            }
            // 创建分页构造器
            PageHelper.startPage(pageIndex,pageSize);
            List<SideData2130> data2130List = sideData2130Mapper.selectList(queryWrapper);
            PageInfo pageInfo = new PageInfo(data2130List);
            return ResultData.ResultDataFaultSelf("查询成功",pageInfo);
        } else if (productionLine.equals("3")) {
            if (!condition.isEmpty()) {
                List<SideData2130Cal> sideData2130CalList = sideData2130CalMapper.findByData((pageIndex - 1) * pageSize, pageIndex * pageSize,condition);
                Integer count = sideData2130CalMapper.findByDataCount((pageIndex - 1) * pageSize, pageIndex * pageSize,condition);
                return ResultData.ResultDataSuccess(sideData2130CalList,count);
            }
        }
        return ResultData.ResultDataFaultSelf("参数传入有误",null);
    }
}
